package controller;

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.ArrayList;


public class LogUserServlet extends HttpServlet {
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
	}
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html");
		String user1 = request.getParameter("user");
		String passwd = request.getParameter("passwd");
		@SuppressWarnings("unchecked")
		ArrayList<model.User> users = (ArrayList<model.User>)getServletContext().getAttribute("users");
		PrintWriter out = response.getWriter();
		for(model.User user0: users){
			if(user0.getName().equals(user1) && user0.getPassword().equals(passwd)){
				HttpSession session = request.getSession();
				if(session.isNew()){
					session.setAttribute("user", user0);
					out.println("<title>Welcome, "+user0.getName()+"!</title>");
					out.println("<h1>Seja Bem Vindo, "+user0.getName()+"!</h1>");
					out.println("<a href=\"./E-COMMERCE/home/index.html\">Entrar na home page</a>");
				}
				else{
					session.getAttribute("user");
					out.println("<h1>Bem vindo novamente, "+user0.getName()+"!</h1>");
					out.println("<a href=\"./E-COMMERCE/home/index.html\">Entrar na home page</a>");
				}
			break;	
			}
		}
	}
}
